package org.ipl.process;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class calculatePoints {
	static Map resultMap = new TreeMap();
	static Map winnerMap = new TreeMap();
	static Map momMap = new TreeMap();
	static Map sixesMap = new TreeMap();
	static Map sixesTeamMap = new TreeMap();
	static List mccPlayers = new ArrayList();
	int unusedPoints = 0;
	
	static String db_driver = "com.mysql.jdbc.Driver";
	static String db_user = "kiran_ipl";
	static String db_password = "lion123";
	static String db_url = "jdbc:mysql://localhost:3306/kiranvenkat_IPL2012";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		String matchId = "12_APR@KXP|PW";
		String league = "MCC";
		
		try {
			populateMCCPlayers();
			Class.forName(db_driver);
			Connection conn = DriverManager.getConnection(db_url, db_user, db_password);
			Statement stmt = conn.createStatement();
			System.out.println("Getting Match Results from DB");
			System.out.print("select winner, mom, sixes, sixesWinner from matchresults where matchId='"+matchId+"'");
			ResultSet rset = stmt.executeQuery("select winner, mom, sixes, sixesWinner from matchresults where matchId='"+matchId+"'");
			System.out.println("Adding Match Results from DB to resultMap");
			while (rset.next()) {
				resultMap.put("matchId" , matchId);
				resultMap.put("winner" , rset.getString(1));
				resultMap.put("mom" , rset.getString(2));
				resultMap.put("sixes" , rset.getString(3));
				resultMap.put("sixesWinner" , rset.getString(4));
			}
			System.out.println("resultMap matchId "+(String) resultMap.get("matchId"));
			System.out.println("resultMap winner "+resultMap.get("winner"));
			System.out.println("resultMap mom "+resultMap.get("mom"));
			System.out.println("resultMap sixes "+resultMap.get("sixes"));
			System.out.println("resultMap sixesWinner "+resultMap.get("sixesWinner"));
			
			System.out.println("Getting Answers of Quiz from DB adding to different map's based on parameter(winner, mom, sixes, sixesTeam)");
			rset = stmt.executeQuery("select winner, mom, sixes, sixesWinner, userName, league from answers where matchId='"+matchId+"' and league in('MCC')");

			while (rset.next()) {
				System.out.println("Voter::"+rset.getString(5));
				System.out.println("Winner::"+rset.getString(1));
				winnerMap.put(rset.getString(5), rset.getString(1));
				momMap.put(rset.getString(5), rset.getString(2));
				sixesMap.put(rset.getString(5), rset.getString(3));
				sixesTeamMap.put(rset.getString(5), rset.getString(4));
				System.out.println("winnerMap Size::"+winnerMap.size());
				System.out.println("momMap Size::"+momMap.size());
				System.out.println("sixesMap Size::"+sixesMap.size());
				System.out.println("sixesTeamMap Size::"+sixesTeamMap.size());
			}
			
			Map winnerPointsMap = processWinner(winnerMap);
			Map momPointsMap = processMom(momMap);
			Map sixesPointsMap = processSixes(sixesMap, sixesTeamMap);
			
			System.out.println("winnerPointsMap Size:"+winnerPointsMap.size());
			System.out.println("momPointsMap Size:"+momPointsMap.size());
			System.out.println("sixesPointsMap Size:"+sixesPointsMap.size());
			
			System.out.println(winnerPointsMap.entrySet());
			System.out.println(momPointsMap.entrySet());
			System.out.println(sixesPointsMap.entrySet());
			
			for (int i=0;i<mccPlayers.size();i++) {
				String player = (String) mccPlayers.get(i);
				System.out.println(player+" winner = " +winnerPointsMap.get(player));
				System.out.println(player+" mom = " +momPointsMap.get(player));
				System.out.println(player+" sixes = " +sixesPointsMap.get(player));
				int total = (Integer)winnerPointsMap.get(player)
				  +(Integer)momPointsMap.get(player)
				  +(Integer)sixesPointsMap.get(player);
				System.out.println(player+" TOTAL = " +total);
				PreparedStatement pstmt = null;
				String query = "insert into points(matchId, winner, mom, sixes, total, voterId, league)values(?,?,?,?,?,?,?)";
				 pstmt = conn.prepareStatement(query); 
				 pstmt.setString(1, matchId);
				 pstmt.setString(2, String.valueOf(winnerPointsMap.get(player)));
				 pstmt.setString(3, String.valueOf(momPointsMap.get(player)));
				 pstmt.setString(4, String.valueOf(sixesPointsMap.get(player)));
				 pstmt.setString(5, String.valueOf(total));
				 pstmt.setString(6, player);
				 pstmt.setString(7, league);
				 
				 pstmt.executeUpdate();
				 pstmt.close();
				 System.out.println("Updated Succesfully");
			}
			
			rset.close();
			stmt.close();
			conn.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
		}    
	}

	private static Map processSixes(Map sixMap, Map sixesTeam) {
		Map sixesPointsMap = new TreeMap();
		List gain = new ArrayList();
		List loss = new ArrayList();
		
		for (int i=0;i<mccPlayers.size();i++) {
			String player = (String) mccPlayers.get(i);
			System.out.println("Player Name: "+player);
			if (!((String)resultMap.get("sixes")).equals((String) sixMap.get(player)) ||
					!((String)resultMap.get("sixesWinner")).equals((String) sixesTeam.get(player)) ){
				loss.add(player);
			}
			else if (((String)resultMap.get("sixes")).equals((String) sixMap.get(player)) &&
					((String)resultMap.get("sixesWinner")).equals((String) sixesTeam.get(player)) ) {
				gain.add(player);
			}
		}
		
		if(gain.size() == 0){
			sixesPointsMap.put("unusedSixesPoints", 80);
		}else if(gain.size() > 0){
			int pointsGained = 80 / gain.size();
			for(int p=0;p<gain.size();p++){
				sixesPointsMap.put(gain.get(p), pointsGained);
				System.out.println(gain.get(p) +":GAIN:"+sixesPointsMap.get(gain.get(p)));
			}
			
		}
		if(loss.size() > 0){
			int pointsLost = -10;
			for(int p=0;p<loss.size();p++){
				sixesPointsMap.put(loss.get(p), pointsLost);
				//System.out.println(gain.get(p) +":LOSS:"+sixesPointsMap.get(gain.get(p)));
			}
		}
		
		return sixesPointsMap;
	}

	private static Map processMom(Map momMap) {
		Map momPointsMap = new TreeMap();
		List gain = new ArrayList();
		List loss = new ArrayList();
		
		for (int i=0;i<mccPlayers.size();i++) {
			String player = (String) mccPlayers.get(i);
			System.out.println("Player Name: "+player);
			if (!((String)resultMap.get("mom")).equals((String) momMap.get(player))){
				loss.add(player);
			}
			else if (((String)resultMap.get("mom")).equals((String) momMap.get(player))) {
				gain.add(player);
			}
		}
		
		if(gain.size() == 0){
			momPointsMap.put("unusedMomPoints", 80);
		}else if(gain.size() > 0){
			int pointsGained = 80 / gain.size();
			for(int p=0;p<gain.size();p++){
				momPointsMap.put(gain.get(p), pointsGained);
				System.out.println(gain.get(p) +":GAIN:"+momPointsMap.get(gain.get(p)));
			}
			
		}
		if(loss.size() > 0){
			int pointsLost = -10;
			for(int p=0;p<loss.size();p++){
				momPointsMap.put(loss.get(p), pointsLost);
				//System.out.println(gain.get(p) +":LOSS:"+momPointsMap.get(gain.get(p)));
			}
		}
		
		return momPointsMap;
	}

	private static Map processWinner(Map winMap) {
		Map winnerPointsMap = new TreeMap();
		List gain = new ArrayList();
		List loss = new ArrayList();
		
		for (int i=0;i<mccPlayers.size();i++) {
			String player = (String) mccPlayers.get(i);
			System.out.println("Player Name: "+player);
			if (!((String)resultMap.get("winner")).equals((String) winMap.get(player))){
				loss.add(player);
			}
			else if (((String)resultMap.get("winner")).equals((String) winMap.get(player))) {
				gain.add(player);
			}
		}
		
		if(gain.size() == 0){
			winnerPointsMap.put("unusedWinnerPoints", 80);
		}else if(gain.size() > 0){
			int pointsGained = 80 / gain.size();
			for(int p=0; p<gain.size(); p++){
				winnerPointsMap.put(gain.get(p), pointsGained);
				System.out.println(gain.get(p) +":GAIN:"+winnerPointsMap.get(gain.get(p)));
			}
			
		}
		if(loss.size() > 0){
			int pointsLost = -10;
			for(int p=0; p<loss.size(); p++){
				winnerPointsMap.put(loss.get(p), pointsLost);
				//System.out.println(loss.get(p) +":GAIN:"+winnerPointsMap.get(loss.get(p)));
			}
		}
		return winnerPointsMap;
	}
	
	/**
	 * @return the mccPlayers
	 */
	public List getMccPlayers() {
		return mccPlayers;
	}


	/**
	 * @param mccPlayers the mccPlayers to set
	 */
	public void setMccPlayers(List mccPlayers) {
		this.mccPlayers = mccPlayers;
	}
	
	private static void populateMCCPlayers() {
		
		try {
			Class.forName(db_driver);
			Connection conn = DriverManager.getConnection(db_url, db_user, db_password);
			Statement stmt = conn.createStatement();
			ResultSet rset = stmt.executeQuery("select userName from users where league='MCC'");

			while (rset.next()) {
				mccPlayers.add(rset.getString(1));
			}
			System.out.println("MccPlayers SIZE "+mccPlayers.size());
			rset.close();
			stmt.close();
			conn.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
		}   
	}

}
